//
//  SupplementsTableViewController.m
//  MyPizzaMobile
//
//  Created by Anton Atanasov on 16.02.13.
//  Copyright (c) 2013 г. Stanislav. All rights reserved.
//

#import "SupplementsTableViewController.h"

@interface SupplementsTableViewController ()
@property (strong) NSDictionary* supplementsDict;
@property (readwrite) RequestManager* requestManager;
@end

@implementation SupplementsTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Pizza Ingredients";
    
    self.requestManager = [RequestManager requestManagerInstance];
    self.supplementsDict = [self.requestManager getAllPizzaSupplements];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.supplementsDict count];
}
-(void)viewDidAppear:(BOOL)animated
{
    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonSystemItemFastForward target:self action:@selector(nextButtonPressed)];
    self.navigationItem.rightBarButtonItem = nextButton;
    [super viewDidAppear:animated];
}
- (SupplementCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SupplementCell *cell = nil;
    
    cell = [tableView dequeueReusableCellWithIdentifier:@"supplementCell"];
    
    if (!cell) {
        cell = [[SupplementCell alloc]init];
    }
    NSString* key = [[NSString alloc]initWithFormat:@"%d",indexPath.row];
    NSDictionary *supplement = (NSDictionary *)[self.supplementsDict objectForKey:key];
    cell.tableViewIndex = [[NSNumber alloc]initWithInt:indexPath.row];
    cell.nameLabel.text =[supplement objectForKey:@"name"];
    cell.identity = [supplement objectForKey:@"id"];
    cell.amount = [supplement objectForKey:@"amount"];
    cell.amountOrdered = 0;
    cell.ppu = [supplement objectForKey:@"pricePerUnit"];
    cell.unitType = [supplement objectForKey:@"unitType"];
    cell.amountLabel.text = @"";
    cell.totalLabel.text = @"";
    [cell refreshData];
        //[cell prepareForReuse];
    return cell;
}

- (void)viewDidUnload {
    [self setSupplementsTableView:nil];
    [super viewDidUnload];
}

- (IBAction)nextButtonPressed
{
    [self performSegueWithIdentifier:@"pushmap" sender:self];
//    double totalPrice =0;
//    for (NSString* key in self.requestManager.orderSupplements)
//    {
//        NSDictionary* temp =[self.requestManager.orderSupplements objectForKey:key];
//        totalPrice = totalPrice + [[temp objectForKey:@"total"] doubleValue];
//    }
//    totalPrice += [[self.requestManager.orderPizzas objectForKey:@"size"] doubleValue]*0.3f;
//    totalPrice += [self.requestManager getShippingPrice];
//    [self.requestManager.orderPizzas setObject:[[NSString alloc] initWithFormat:@"%.2f",totalPrice] forKey:@"totalPrice"];
//    NSString* alertMessage = [[NSString alloc]initWithFormat:@"Do you want to order pizza with size %@ cm. to address %@ for total price $%.2f",[self.requestManager.orderPizzas objectForKey:@"size"],[self.requestManager.orderPizzas objectForKey:@"orderAddress"],totalPrice];
//    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Order"
//                                                      message:alertMessage
//                                                     delegate:self
//                                            cancelButtonTitle:@"Cancel"
//                                            otherButtonTitles:@"OK", nil];
//    [message show];
////    [self performSegueWithIdentifier:@"chooseSupplements" sender:self];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex)
    {
        if([self.requestManager orderPizza]) [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
@end
